package com.qhg.saas_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.mapper.ProductSalesMapper;
import com.qhg.saas_server.pojo.dto.ProductSalesDto;
import com.qhg.saas_server.service.ProductSalesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ProductSalesServiceImpl implements ProductSalesService {

    @Autowired
    ProductSalesMapper productSalesMapper;


    /**
     * 根据条件查询ProductSales
     * @param productSalesDto
     * @return
     */
    @Override
    public PageInfo<ProductSalesDto> getProductSalesByConditon(ProductSalesDto productSalesDto) {

        String sortRule ="ASC";

        //判断排序方式方式，按照销售额排序
        Integer rule = productSalesDto.getSortRule();
        if(!Objects.isNull(rule)){
            sortRule = rule == 1 ? "DESC" : "ASC";
        }

        //设置默认的serchRadius 如果没有参数则默认查询本日记录
        Integer serchRadius = productSalesDto.getSerchRadius();
        if(Objects.isNull(serchRadius)){
            serchRadius = 1;
        }

        //开启分页插件
        PageHelper.startPage(productSalesDto.getPageNum(),productSalesDto.getPageSize(),"salesVolume  "+sortRule);

       List<ProductSalesDto> list =  productSalesMapper.getProductSalesByConditon(productSalesDto);

        return new PageInfo<>(list);
    }
}
